package leetcode.f901t1000;

/**
 * TODO
 *
 * @author lichx
 * @date 2021/9/15 9:11
 */
public class ReverseOnlyLetters_917 {
    public static void main(String[] args) {
        System.out.println(reverseOnlyLetters("Test1ng-Leet=code-Q!"));
    }

    public static String reverseOnlyLetters(String s) {
        char[] c = s.toCharArray();
        char[] r = new char[c.length];
        int i = 0,j = c.length-1;
        while(i<c.length){
            while(i<c.length && !isLatter(c[i])) {
                r[i] = c[i++];
            }
            while(j>0 && !isLatter(c[j])) {
                j--;
            }
            if(i<c.length && j >= 0){
                r[j--] = c[i++];
            }
        }
        return new String(r);
    }
    public static boolean isLatter(char a){
        return a>='a' && a<='z' || a>='A' && a<='Z';
    }
}
